package com.chinda.common.page;

import cn.hutool.core.util.StrUtil;
import com.chinda.common.xss.SQLFilter;
import com.querydsl.core.types.Predicate;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

import java.io.Serializable;

/**
 * @author Wang Chinda
 * @date 2020/8/8
 * @see
 * @since 1.0
 */
@Getter
@Setter
public abstract class BaseQO implements Serializable {
    private static final long serialVersionUID = -45200812016536165L;
    /**
     * 每页显示多少条
     */
    private Integer pageSize = 20;
    /**
     * 当前页码
     */
    private Integer currPage = 1;
    /**
     * 排序
     */
    private String order;
    /**
     * 排序字段
     */
    private String orderField;

    public PageRequest getPageRequest() {
        orderField = SQLFilter.sqlInject(orderField);
        Sort.Direction direction = Sort.Direction.ASC;
        if (StrUtil.isNotBlank(orderField)) {
            if (StrUtil.equalsIgnoreCase(order, Sort.Direction.DESC.name())) {
                direction = Sort.Direction.DESC;
            }
            return PageRequest.of(currPage - 1, pageSize, Sort.by(direction, orderField));
        }
        return PageRequest.of(currPage - 1, pageSize);
    }

    /**
     * 获取动态sql
     * @return
     */
    public abstract Predicate getPredicate();
}
